package com.Bigbluebook.Service;

import com.Bigbluebook.api.ApiConstants;
import com.Bigbluebook.entity.Comment;
import com.Bigbluebook.entity.Post;
import com.Bigbluebook.entity.PostsInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PostApiService {
    private PostService postService;
    private Retrofit retrofit;
    private Gson gson;

    public PostApiService(){
        gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
                .create();
        retrofit = new Retrofit.Builder()
                .baseUrl(ApiConstants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        postService = retrofit.create(PostService.class);
    }

    public  void createPost(Post post, Callback<Post> callback){
        Call<Post> call = postService.createPost(post);
        call.enqueue(callback);
    }

    public void getAllPosts(Callback<List<Post>> callback) {
        Call<List<Post>> call = postService.getAllPosts();
        call.enqueue(callback);
    }

    public void updatePost(int postId, Post post, Callback<Post> callback) {
        Call<Post> call = postService.updatePost(postId, post);
        call.enqueue(callback);
    }

    public void deletePost(int postId, Callback<Void> callback) {
        Call<Void> call = postService.deletePost(postId);
        call.enqueue(callback);
    }

    //获取帖子详情
    public void getPostById(int postId, Callback<PostsInfo> callback) {
        Call<PostsInfo> call = postService.getPostById(postId);
        call.enqueue(callback);
    }

    //获取评论列表
    public void getCommentsByPostId(int postId, Callback<List<Comment>> callback) {
        Call<List<Comment>> call = postService.getCommentsByPostId(postId);
        call.enqueue(callback);
    }

    public void getLikesCount(int postId, Callback<Long> callback) {
        Call<Long> call = postService.getLikesCount(postId);
        call.enqueue(callback);
    }

    public void getStarsCount(int postId, Callback<Long> callback) {
        Call<Long> call = postService.getStarsCount(postId);
        call.enqueue(callback);
    }
}
